package com.quick.calcu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quick.calcu.entity.ReviewPlan;
import com.quick.calcu.entity.Student;
import com.quick.calcu.entity.WrongQuestion;
import com.quick.calcu.mapper.ReviewPlanMapper;
import com.quick.calcu.service.ReviewPlanService;
import com.quick.calcu.service.StudentService;
import com.quick.calcu.service.WrongQuestionService;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReviewPlanServiceImpl extends ServiceImpl<ReviewPlanMapper, ReviewPlan> implements ReviewPlanService {

    @Autowired
    private StudentService studentService;

    @Autowired
    private WrongQuestionService wrongQuestionService;

    @Override
    public List<ReviewPlan> getPlansByStudentId(Long studentId) {
        LambdaQueryWrapper<ReviewPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReviewPlan::getStudentId, studentId);
        return list(wrapper);
    }

    @Override
    public ReviewPlan getWithDetails(Long planId) {
        ReviewPlan plan = getById(planId);
        if (plan == null) {
            return null;
        }

        Student student = studentService.getById(plan.getStudentId());
        WrongQuestion wrongQuestion = wrongQuestionService.getById(plan.getWrongId());

        plan.setStudent(student);
        plan.setWrongQuestion(wrongQuestion);

        return plan;
    }

    @Override
    @Transactional
    public boolean updateReviewCount(Long planId, Integer increment) {
        ReviewPlan plan = getById(planId);
        if (plan == null) {
            return false;
        }

        plan.setCount(plan.getCount() + increment);
        return updateById(plan);
    }
}
